
#include <dbg/ptest.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>



#include <metalc/base/mtls.h>


#include <metalc/mlos/tsysmalloc.h>
#include <unistd.h>
#include <stdio.h>
/* tapdsnode, 如果使用就需要 */
// static tstack_t apdstinfo_tlsstk;
// static TLS_INITIAL apdstls_t *apdstinfo_tls;
// DEF_APDSTINFO_TLS(&apdstinfo_tlsstk, &apdstinfo_tls, malloc, 4096, mp_coreid)
static TLS_INITIAL spobj_t *lknode_tls;
DEF_LOCKNODE_SPSLAB(splknode, &lknode_tls, HDEF_CACHE_LINE_BYTES, 16384)

/* 128KB, 可以容纳多少个线程? */
DEF_POBJ_PHEAP(mlcc, 128 * 1024)
SAI void *mlcc_palloc(size_t size){ return pheap_palloc_mlcc(size); }

/* 前面分配对象, 后面分配mlcc空间 然后在使用巨页*/
// DEF_APDSNODE_SPSLAB(tapdsnode, HDEF_CACHE_LINE_BYTES, 2 * 1024 * 1024)
// SAI void *apdsnode_alloc(size_t size){ if (size <= objsize) return (void *)objname##_cachealloc(); return NULL; }\
// SAI void apdsnode_free(void *ptr, size_t size){ objname##_cachefree((spobj_t*)ptr); }
// SAI void *mlcc_palloc(size_t size){ return tapdsnode_spalloc_tail(size); }

// /* tapds 2M内存池 一开始就使用巨页 */
// DECL_INIT(hugepage)
// SAI void hugepage_procinit(){
//	 if (madvise(tapdsnode_pool_arr, 2 * 1024 * 1024, MADV_HUGEPAGE) != 0) fprintf(stderr, "hugepage mapping failed\n");
// }
// SAI void hugepage_threadinit(){
// }


COMPOSE_MLAYER(testallocator, tsysmalloc)
DEF_MTLS_PTHREAD(GET_MLAYER_OBJNR(testallocator), 4096, mlcc_palloc, mp_coreid)

/* 初始化过程 */
// DEF_INIT(testallocator, mtls, splknode)

#include <pthreadalt/pthreadalt_init.h>
DEF_PTHREADALT_INIT
DEF_SYSINIT(testallocator, mtls)
DEF_CFLOWINIT(testallocator, mtls)
DEF_CFLOWFIN(mtls, splknode)
DEF_SYSFIN(__omega)


void test_mtls(){

}

void test_req(){

}

void test_tsysmalloc(){
	mreq_t req;
	mreq_setalloc(&req, sizeof(word_t), 0);
	void *mem = testallocator_alloc(&req);
	__memset(mem, 0, sizeof(word_t));
	mreq_setfree(&req, mem, sizeof(word_t));
	testallocator_free(&req);
}

int main(){

	test_tsysmalloc();
	return 0;
}

/* 测试cobj_t */
/* 测试tlsf */
